﻿using System;
using Assets.Scripts.Net.Data.Model;
using UnityEngine;
using UnityEngine.UI;
using Assets.Scripts.UI.Panel;
using System.Text;

namespace Assets.Scripts.UI.Item
{

    public class AnsSubItem:UIBase<AnsSubItem>
    {
        public InputField InputField;
        public Text Placeholder;
        public Text ScoreText;
        public Image RightLable;
        public Image WrongLable;
        public Transform Content;
        private LayoutElement layout;

        private Action<bool> action { get; set; }//定义一个委托

        private AnsInfo ansInfo { get; set; }

        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            layout = transform.GetComponent<LayoutElement>();
            AutoFindComponent(transform,true);
        }


#if TEST
        protected override void InitOnDestroy()
        {
            ExamPanel.OnRandomAnswer -= OnRandomAnswer;
        }

        private void OnRandomAnswer()
        {
            System.Random random = new System.Random();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 1000; i++)
            {
                sb.Append("这是一个测试答案 ");
            }
            InputField.text = sb.ToString();
        }
#endif
        public void Init(AnsInfo info,int queType,Action<bool> action,bool isCheckScore,QueInfo queinfo)    
        {
#if TEST
            ExamPanel.OnRandomAnswer += OnRandomAnswer;
#endif
            Content.gameObject.SetActive(true);//激活显示
            ansInfo = info;
            this.action = action;
            switch (queType)
            {
                case 3://填空
                    layout.preferredHeight = 130;
                    Placeholder.text = "请输入答案...";
                    //InputField.characterLimit = 30;
                    break;
                case 4://简答
                    layout.preferredHeight = 400;
                    Placeholder.text = "请输入答案...";
                    //InputField.characterLimit = 150;
                    break;
            }
            InputField.interactable = !isCheckScore;
            //Debug.Log("是否是查询成绩："+isCheckScore);
            if (isCheckScore)
            {
                ScoreText.text = queinfo.gainscore.ToString();
                InputField.text = ansInfo.userans;
                RightLable.gameObject.SetActive(false);
                WrongLable.gameObject.SetActive(false);
                InputField.transform.Find("Placeholder").gameObject.SetActive(false);
            }
            else
            {
                ScoreText.gameObject.SetActive(false);
                RightLable.gameObject.SetActive(false);
                WrongLable.gameObject.SetActive(false);
                InputField.onValueChanged.AddListener(x =>
                {
                    ansInfo.userans = x;
                    ansInfo.ischoose = !string.IsNullOrEmpty(x);
                    if (action != null) action(string.IsNullOrEmpty(x));
                });
            }
        }
    }
}